package com.cloudcc.mobile.util;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudcc.mobile.R;

/**
 * Created by lin on 2017/7/12.
 */

public class ToastUtil {

    private Toast toast;
    private LinearLayout toastView;
    private TextView textview;
    private ImageView imageView;
    private LinearLayout layoutAll;

    /**
     * 修改原布局的Toast
     */
    public ToastUtil(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.toast_view, null);
        layoutAll = (LinearLayout) view.findViewById(R.id.toast_view_root);
        textview = (TextView) view.findViewById(R.id.toast_view_text);
        imageView = (ImageView) view.findViewById(R.id.toast_view_image);

        int w = (int) (context.getResources().getDisplayMetrics().widthPixels - SystemUtils.dpToPx(14, context));
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(w, WindowManager.LayoutParams.MATCH_PARENT);
        layoutAll.setLayoutParams(layoutParams);
        toast = new Toast(context);
        toast.setView(view);
    }

    /**
     * 完全自定义布局Toast
     *
     * @param context
     * @param view
     */
    public ToastUtil(Context context, View view, int duration) {
        toast = new Toast(context);
        toast.setView(view);
        toast.setDuration(duration);
    }

    /**
     * 向Toast中添加自定义view
     *
     * @param view
     * @param postion
     * @return
     */
    public ToastUtil addView(View view, int postion) {
        toastView = (LinearLayout) toast.getView();
        toastView.addView(view, postion);

        return this;
    }

    public ToastUtil setTextNoImage(String text) {
        imageView.setVisibility(View.GONE);
        textview.setText(text);

        return this;
    }

    /**
     * 短时间显示Toast
     */
    public ToastUtil Short(Context context, CharSequence message) {
        if (toast == null || (toastView != null && toastView.getChildCount() > 1)) {
            toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
            toastView = null;
        } else {
            toast.setDuration(Toast.LENGTH_SHORT);
        }
        return this;
    }

    /**
     * 长时间显示Toast
     */
    public ToastUtil Long(Context context, CharSequence message) {
        if (toast == null || (toastView != null && toastView.getChildCount() > 1)) {
            toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
            toastView = null;
        } else {
            toast.setDuration(Toast.LENGTH_LONG);
        }
        return this;
    }

    /**
     * 自定义显示Toast时间
     *
     * @param context
     * @param message
     * @param duration
     */
    public ToastUtil Indefinite(Context context, CharSequence message, int duration) {
        if (toast == null || (toastView != null && toastView.getChildCount() > 1)) {
            toast = Toast.makeText(context, message, duration);
            toastView = null;
        } else {
            toast.setDuration(duration);
        }
        return this;
    }

    /**
     * 显示Toast
     * @param topLong  距离上边缘的距离单位DP
     * @param context  上下文
     * @return
     */
    public ToastUtil show(int topLong,Context context) {
        toast.setGravity(Gravity.TOP, 0 , (int) SystemUtils.dpToPx(topLong,context));
        toast.show();

        return this;
    }

    /**
     * 显示Toast上下居中
     * @return
     */
    public ToastUtil show() {
        toast.setGravity(Gravity.CENTER, 0 , 0);
        toast.show();
        return this;
    }

    /**
     * 获取Toast
     *
     * @return
     */
    public Toast getToast() {
        return toast;
    }


}
